package com.niiwoo.civet.user.service.local;

import com.niiwoo.civet.user.dao.aggregation.UserRealNameInfo;
import com.niiwoo.civet.user.dao.entity.UserBlackList;
import com.niiwoo.civet.user.dao.mapper.UserBasicInfoMapperExt;
import com.niiwoo.civet.user.dao.mapper.UserBlackListMapperExt;
import com.niiwoo.civet.user.enums.BlackEnum;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Author:LiJian
 * @Date:2017/11/9
 */
@Service
public class UserBlackListLocalService {


    @Autowired
    private UserBlackListMapperExt userBlackListMapper;

    @Autowired
    private UserBasicInfoMapperExt userBasicInfoMapperExt;

    /**
     * 获取用户黑灰名单列表
     * @param userId
     * @return
     */
    public List<UserBlackList> getUserBlackList(String userId) {
        UserRealNameInfo userRealNameInfo = userBasicInfoMapperExt.getUserRealNameInfo(userId);
        List<UserBlackList> userBlackLists = new ArrayList<>();
        String mobileNo = userRealNameInfo.getMobileNo();
        if(StringUtils.isNotBlank(mobileNo)) {
            List<UserBlackList> subList1 = userBlackListMapper.getBlackListByIdCardOrMobileNo(null, mobileNo);
            userBlackLists.addAll(subList1);
        }
        if(StringUtils.isNotBlank(userRealNameInfo.getIdCard())) {
            List<UserBlackList> subList2 = userBlackListMapper.getBlackListByIdCardOrMobileNo(userRealNameInfo.getIdCard(), null);
            if(StringUtils.isNotBlank(mobileNo)) {
                for(UserBlackList userBlackList : subList2) {
                    if(!mobileNo.equals(userBlackList.getMobileNo())) {
                        userBlackLists.add(userBlackList);
                    }
                }
            }else {
                userBlackLists.addAll(subList2);
            }
        }

        return userBlackLists;
    }

    /**
     * 查询用户单个黑名单
     * @param userId
     * @return
     */
    public UserBlackList getSingleBlackListInfo(String userId){
        UserBlackList userBlackList = null;
        List<UserBlackList> userBlackListList = this.getUserBlackList(userId);
        if(userBlackListList != null && userBlackListList.size() > 0) {
            userBlackList = userBlackListList.get(0);
            // 黑名单优先
            for(UserBlackList blackList : userBlackListList) {
                if(BlackEnum.BlackType.BLACK_LIST.getType() == blackList.getListType()) {
                    userBlackList = blackList;
                    break;
                }
            }
        }

        return userBlackList;
    }

    /**
     * 根据手机号获取黑灰名单
     * @param mobileNo
     * @return
     */
    public List<UserBlackList> getUserBlackListByMobileNo(String mobileNo) {
        List<UserBlackList> subList = userBlackListMapper.getBlackListByIdCardOrMobileNo(null, mobileNo);
        return subList;
    }

    /**
     * 根据身份证号获取黑灰名单
     * @param idCard
     * @return
     */
    public List<UserBlackList> getUserBlackListByIdCard(String idCard) {
        List<UserBlackList> subList = userBlackListMapper.getBlackListByIdCardOrMobileNo(idCard, null);
        return subList;
    }
}
